﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernateDemo.DAL.Entity;
using NHibernateDemo.DAL;
using Arich.Data.GenericNHDal;
namespace MvcAppDemo.Web.Controllers {
    public class PersonController : Controller {
        //
        // GET: /Person/
        [OutputCache(Duration = 5)]
        public ActionResult List( ) {

            var dal = new PersonBasicDAL( );
            var ps = dal.GetAll( ).ToList( );
            return View("PersonList", ps);


        }

        //
        // GET: /Person/Details/5

        public ActionResult Details(int id) {
            return View( );
        }

        //
        // GET: /Person/Create

        public ActionResult Create( ) {
            return View( );
        }

        //
        // POST: /Person/Create

        [HttpPost]
        public ActionResult Create(PersonBasic person) {
            try {
                // TODO: Add insert logic here
                var dal = new PersonBasicDAL( );
                dal.Save(person);
                return RedirectToAction("List");

            } catch {
                return View( );
            }
        }

        //
        // GET: /Person/Edit/5

        public ActionResult Edit(int id) {
            return View( );
        }

        //
        // POST: /Person/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection) {
            try {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            } catch {
                return View( );
            }
        }

        //
        // GET: /Person/Delete/5
        [HttpPost]
        public ActionResult Delete(int id) {
            try {
                var dal = new PersonBasicDAL( );
                dal.DeleteById(id);

                return Json(new { success = true });
            } catch (Exception e) {
                return Json(new { success = false, msg = e.Message });
            }
        }

        //
        // POST: /Person/Delete/5

        //[HttpPost]
        //public ActionResult Delete(int id) {
        //    try {
        //        // TODO: Add delete logic here
        //        return JavaScript("alert('1')");
        //        //return RedirectToAction("PersonList");
        //    } catch {
        //        //return View();
        //        return JavaScript("alert('2')");
        //    }
        //}
        public ActionResult Index() {
            throw new NotImplementedException();
        }
    }
}
